package practice;

/**
 * @ClassName PrintTakeTurn
 * @Description 线程交替打印
 * @Company inspur
 * @Author Kevin
 * @Date 2020/7/11 16:40
 * @Version 1.0
 */
public class PrintTakeTurn {

    class Print implements Runnable {

        volatile Integer i = 1;

        @Override
        public void run() {
            while (i < 100) {
                synchronized (this) {
                    System.out.println(Thread.currentThread().getName() + "i :" + i++);
                    notify();
//                    try {
//                        Thread.currentThread();
//                        // 使其休眠100毫秒，放大线程差异
//                        Thread.sleep(100);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }


    public static void main(String[] args) {
        Runnable r = new PrintTakeTurn().new Print();
        Thread a = new Thread(r);
        Thread b = new Thread(r);

        a.start();
        b.start();
    }
}
